
// 注册，获取表单数据作为参数，发起注册请求，处理响应数据
const onRegister = function () {
    // 获取表单输入框
    const register = document.querySelector('.register_form')
    const userInput = document.querySelector('input[name="phone"]')
    const passwordInput = document.querySelector('input[name="password"]')
    const addressInput = document.querySelector('.address')

    // 获取表单提示框
    const userP = document.querySelector('.user_msg')
    const pwdP = document.querySelector('.pwd_msg')
    const registerP = document.querySelector('.register_msg')
    register.addEventListener('submit', async function (e) {
        // 阻止表单默认提交行为
        e = e || window.event
        e.preventDefault()

        // 获取表单输入的值
        let username = userInput.value
        let password = passwordInput.value
        // sha256密码加密处理
        password = sha256(password)
        let address = addressInput.value
 
        // 表单非空验证
        if (username == '') {
            userP.innerHTML = '手机号不能为空！'
            return false
        } else {
            userP.innerHTML = ''
        }
        if (password == '') {
            pwdP.innerHTML = '验证码不能为空！'
            return false
        } else {
            pwdP.innerHTML = ''
        }
        if (address == '中国') {
            if (confirm('默认当前地址为中国？') == false) {
                return false
            }
        }
        // 调用注册接口
        // let formData = new FormData()
        // formData.append('username',username)
        // formData.append('password', sha256(password))
        // formData.append('address', address)
        
        // console.log(formData);

        let res = await myAxios({
            method: 'post',
            url: '/users/register',
            // headers: { 'content-type': 'application/x-www-form-urlencoded' },
            // data:formData,
            data: {
                username,
                password,
                address,
            }
        })
        if (res.data.code == 1) {
            registerP.innerHTML = '注册成功！'
            // console.log(username);
            // console.log(password);
            // console.log(address);
            // console.log('formdata:',formData);
            // console.log('用户名',formData.get('username')); 
        }
    })
}